# This control file and associated files replicate  the findings for "Foreign Aid Funnel? 
# Placebo-Based Assessment of Aid Flows to  Non-Permanent U.N. Security Council Members"
# Evangeline Reynolds (University of Illinois)
# Matthew Winters (University of Illinois)
# Evangeline Reynolds wrote and notated the code.  
# Matt Winters checked and additionally annotated the code.


# This file executes subordinate files in which all data manipulation and R-based analysis occurs.

# Using this R script, you will be able to  
# (1) clean the U.N. Security Council data, 
# (2) create the placebo U.N. Security Councils,
# (3) merge the placebo Security Councils with the data from Vreeland and Dreher, and 
# (5) analyze the output from regression models using the permuted data.

# To (4) run the regression models, use Stata.  The code to execute the .do files corresponding
# to that step are provided but must be executed in Stata.

# Through knitr and rmarkdown, we create dynamic documents.  Both of these packages will need to be
# installed in order to fully replicate our original analysis.

# The default in this control file is to knit-to-pdf.  Below, the user might choose to instead
# knit-to-html.  Kniting to pdf requires the use of LaTeX and may require loading additional packages
# in LaTeX.  Therefore, for non-LaTeX users, knit-to-html is recommended. 

# In order to facilitate readers walking through the analysis without running all of the code below, 
# we have included our output PDFS in the folder "100CodeRecords".

# Clear objects from memory
rm(list=ls())

# To use this file interactively, first set your working directory to the location 
# where this control file is located. 
# setwd(????)


############## Prepare file from DataVerse #######################################
# First you will need to download files from dataverse into one directory.
# You can download all files except .tab files as batch download
# The .tab files must be downloaded one-by-one and in their original file format

### Dataverse renamed some files (we saved out .RData and .csv versions of the same file)
### Use this code to convert back to the original names. 
#install.packages("stringr")  #install if not yet installed.
library(stringr)
CSVs=list.files(pattern ="-1.csv")
NewNames=str_replace(CSVs, "-1.csv", ".csv")
file.rename(CSVs, NewNames)


#### Organize files into folders ####### 

#directories to be created.
directory<-
  c("./000MyCountryCoder"    ,"./01rawdata" ,"./01VreelandAndDreher" ,
    "./100CodeRecords"   ,"./99_CSVs_of_Output_Data" 
  )

NumFolders=length(directory)

#create directories
for (i in directory){
  if(!dir.exists(i)){dir.create(i)}
}

#list files to be moved to each folder
FilesInFolders=list()

FilesInFolders[[1]]=
  c("countrycode2.R" ,    "MyCountryNamer.csv")

FilesInFolders[[2]]=
  c("UN Member States - CurrentStatesYearJoinUN.txt"
    ,"UN Member States - Detail.txt"
    ,"UN Member States - Members and Join Dates.txt"
    ,"UNRegions - League of Arab States.txt" 
    ,"UNRegions - PreviousCountryRegionalGroups.csv"
    ,"UNRegions - UNModernCountryRegions.txt"
    ,"UNSC - RawDataUNSCnonPerm.txt"   )                                          

FilesInFolders[[3]]=
  c("Chapter 5.do"
    ,"VreelandAndDreherChapter5.dta")

FilesInFolders[[4]]=
  c("02_CleanRawUNMembershipTimeSpans.pdf" 
    ,"02_CleanRawUNRegionCountries.pdf" 
    ,"02_CleanRawUNSCMembershipData.pdf"
    ,"04_Permutation.pdf"
    ,"06_MergeChap5DataAndPermutations.pdf"
    ,"08_Vreeland_&_Dreher_2014_chapter5_W_Permutations.log"
    ,"08_VreelandAndDreherExactReplication.log"
    ,"10_AnalysisPermutationModelResults.pdf"     )          

FilesInFolders[[5]]=
  c("03_UNMemLong.csv" 
    , "03_UNRegions - DATAAllRegionsLong.csv"
    , "03_UNSC - SCTenuresData.csv" 
    , "05_LongSCPermutations.csv"    )

for (i in 1:NumFolders){
  
  for (j in 1:length(FilesInFolders[[i]])){
    
    #Move Folders from the main directory to correct directory
    file.rename(FilesInFolders[[i]][j], 
                paste0(directory[i],"/", FilesInFolders[[i]][j]) )  
    
  } 
  
}



#########################################################
## 0. Info about the R Session  #########################
#########################################################

# First, we report the characteristics of the R session on which we have most recently 
# run this replication.

sessionInfo()

# R version 3.2.2 (2015-08-14)
# Platform: i386-w64-mingw32/i386 (32-bit)
# Running under: Windows 7 x64 (build 7601) Service Pack 1
# 
# locale:
#   [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.1252    
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# loaded via a namespace (and not attached):
#   [1] tools_3.2.2



# Note that the folder 000MyCountryCoder contains an original function and data to assign standard names 
# to countries. This is used in cleaning the raw data and merging the Vreeland and Dreher data with the 
# permuted data.



### Load Preliminaries ####

# If relevant packages are not already installed, install the following: 
# install.packages("knitr")
# install.packages("rmarkdown")
# install.packages("xtable")
# install.packages("stringr")

# load relevant packages:
library(knitr)
library(rmarkdown)
library(xtable)
library(stringr)

#### Set Knit Options ####

# There is code that checks on the transformation of the data throughout, such as 
# head(), tail(), dim(), str(), etc.  
# This output can be supressed by setting results to "hide" in the following command:
opts_chunk$set(results ="markup", echo=TRUE) 

# You can choose to knit to PDF or to HTML
# We recommend choosing "html_document" here though we have used to render pdfs.
# Render with the doc.type.out set to "pdf_document" uses LaTeX.
doc.type.out="pdf_document"      # the other option is "html_document"

#################################################################
#######2. Data Cleaning #########################################
#################################################################

# Markdown files describing the data cleaning and merging process are saved in the cleaning folder.

render("02_CleanRawUNRegionCountries.rmd"    , output_dir = "./100CodeRecords", output_format = doc.type.out )
render("02_CleanRawUNMembershipTimeSpans.rmd", output_dir = "./100CodeRecords", output_format = doc.type.out )
render("02_CleanRawUNSCMembershipData.rmd"   , output_dir = "./100CodeRecords", output_format = doc.type.out )

# The clean data are saved out both as .RData and flat text files (.csv). 

# The RData files are  "03_UNMemLong.RData",  "03_UNRegions - DATAAllRegionsLong.RData"
# and "03_UNSC - SCTenuresData.RData".  Analogous CSVs originally are saved in the main 
# project folder but are moved to a folder to tidy up. 

#################################################################
#######3. The Randomization of SC Data ##########################
#################################################################

# The markdown file that conducts the permutation is "04_Permutation.rmd".

# For a large number of permutations, this process will be quite time consuming.
# A test run of 15 iterations takes approximately five minutes to complete on our platform.
# Note that a series of error messages will be generated for attempted permutations 
# that violate the rules described in the paper.
# However this does not indicate a problem with the code.  
# The loop will disguard the permutation attempt and make another attempt.

# First, we set the number of permutations to be created a value.
# You may set the permutations to minimum 10.  
# For the analysis in the paper, we use 1,000. 
NumPermutation=1000
render("04_Permutation.rmd", output_dir = "./100CodeRecords", output_format=doc.type.out)

# The resultant data is "05_LongSCPermutations.RData".


#############################################################################
## Now we work with Vreeland and Dreher's replication files

# Files from Vreeland and Dreher are "Chapter 5.do" and "VreelandAndDreherChapter5.dta"

# Now we merge the Permutations Data with Vreeland and Dreher's Data
render("06_MergeChap5DataAndPermutations.rmd", output_dir = "./100CodeRecords", output_format=doc.type.out)

# This produces the file "07_Ch5wPerm.dta".

# ****************************************************************************************************
# ******4. Model Estimation in Stata *****************************************************************
# ******The following do commands should be run in STATA *********************************************
# ****************************************************************************************************
#  * In Stata, change the working directory to the location of this readme file
#  cd "???"
#
#  * We use a rewritten version of the replication .do file that we obtained from Vreeland and Dreher:
#  do "08_Chapter 5 - Rewrite - simplify.do"
#  
#  * Using the same code, we reestimate their models with the permuted data
#  do "08_Chapter 5 - With Permutations.do"
#  
#  *This produces the Results
#  *"09_PermuteModelResults.dta"
# ****************************************************************************************************
# ****************************************************************************************************


#########################################################
###5. We process these Results Back in R#################
#########################################################

render("10_AnalysisPermutationModelResults.rmd", 
       output_dir = "100CodeRecords", output_format=doc.type.out)

## The Results look a bit tidier using a .rnw file which can only be knit to pdf.
knit2pdf("10_AnalysisPermutationModelResults.rnw")

## We present a subset of the results to include in the paper:
knit2pdf("10_AccompanyingFiguresAndTables.rnw")



#########################################################
### Tidying up project folder ###########################
#########################################################

## We move .CSVs to a separate folder to clean up #######

CSVs=list.files(pattern="\\.csv")
for (i in CSVs){file.copy(from=i, to=paste0("99_CSVs_of_Output_Data/", i), 
                          overwrite = TRUE, recursive = FALSE,
                          copy.mode = TRUE, copy.date = FALSE)}
CSVs
file.remove(CSVs)

#########################################################
### Remove intermediary files between RNW and PDF #######
#########################################################
for (i in c("\\.aux", "\\.tex", "\\.log", "\\.bbl","\\.gz")){
  
  Files=list.files(pattern=i, ignore.case = TRUE);print(Files)
  file.remove(Files)
}








